package com.chinatelecom.otms.circuitcheck.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * http请求工具类
 */
public class HttpUtil {

    private static RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(120000)
            .setSocketTimeout(120000).setConnectTimeout(120000).build();

    public static String httpGet(String url) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        CloseableHttpResponse response = null;
        String result = "";
        try {
            response = httpClient.execute(httpGet);
        } catch (IOException e) {
            System.out.println("链接超时");
            result = "链接超时";
            return result;
        }
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity,"UTF-8");
        return result;
    }

    //Http协议Post
    public static String httpPost (String url, String json) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type","application/json");
        httpPost.setEntity(new StringEntity(json));
        httpPost.setConfig(requestConfig);
        CloseableHttpResponse response = null;
        String result = "";
        try {
            response = httpClient.execute(httpPost);
        } catch (IOException e) {
            System.out.println("链接超时");
        }
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity,"UTF-8");
        return result;
    }

    public static String getDescByResponseStr(String result){
        JSONObject jsonObject = JSON.parseObject(result);
        JSONObject responseHeader = jsonObject.getJSONObject("responseHeader");
        return responseHeader.getString("desc");
    }

    public static String postInterfaceBySncAndEms(String url, String body, String sncRef, String ems_id) throws IOException {
        String[] params = {ems_id, sncRef};
        String getRoute_body_params = String.format(body, params);
        return HttpUtil.httpPost(url, getRoute_body_params);
    }

    public static String postTopoInterfaceByEmsAndNe(String url, String body, String ems_id, String ne_id) throws IOException {
        String[] params = {ems_id, ne_id};
        String getTopo_body_params = String.format(body, params);
        return HttpUtil.httpPost(url, getTopo_body_params);
    }

    public static String postGetAllEquipments(String url, String body, String ems_id, String ne_id) throws IOException {
        String[] params = {ne_id, ems_id};
        String getTopo_body_params = String.format(body, params);
        return HttpUtil.httpPost(url, getTopo_body_params);
    }

    public static String postGetAllPhysicalTerminationPointsWithoutFtps(String url, String body, String ems_id, String ne_id) throws IOException {
        String[] params = {ne_id, ems_id};
        String getTopo_body_params = String.format(body, params);
        return HttpUtil.httpPost(url, getTopo_body_params);
    }

    public static String postGetFDFR(String url, String body, String ems_id, String ne_id) throws IOException {
        String[] params = {ne_id, ems_id};
        String getTopo_body_params = String.format(body, params);
        return HttpUtil.httpPost(url, getTopo_body_params);
    }

    public static String postGetFDFRRoute(String url, String body, String ems_id, String ne_id) throws IOException {
        String[] params = {ne_id, ems_id};
        String getTopo_body_params = String.format(body, params);
        return HttpUtil.httpPost(url, getTopo_body_params);
    }
}
